package com.itheima.service.aopservice.impl;

import com.itheima.pojo.Setmeal;
import com.itheima.service.aopservice.RedisSyncDataService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;

import java.lang.reflect.Method;

/**
 * <p>health107</P>
 *
 * @author wuguang
 * @date 2020/8/6 9:05
 */
@Service
@Aspect
public class RedisSyncDataServiceImpl implements RedisSyncDataService {
	/*@Autowired
	private JedisPool jedisPool;*/

	@Autowired
	private RedisTemplate<String,Object> redisTemplate;

	/**
	 * 监控检查项服务实现类内跟删除和修改相关方法
	 * 一旦执行这两种方法,立即清除redis内存储的套餐详情数据
	 *
	 *
	 * @param joinPoint 连接点:检查项服务实现类内的方法
	 * @return java.lang.Object
	 * @author wuguang
	 * @date 2020/8/6 11:32
	 *
	 */

	@Override
	@Around("execution(* com.itheima.service.impl.CheckItemServiceImpl.*(..))||" +
			"execution(* com.itheima.service.impl.CheckGroupServiceImpl.*(..))")
	public Object checkItemAndCheckGroupServiceAround(ProceedingJoinPoint joinPoint) {
		//连接点获取方法签名
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();
		//获取方法名
		String methodName = method.getName();


		try {
			System.out.println("环绕-前置正常通知:"+methodName+"开始被执行增强");
			//先对方法进行正常的执行操作
			Object object = joinPoint.proceed();
			//判断方法名只要和增删改有关就将redis内数据进行同步,后置环绕通知进行增强
			if(methodName.contains("delete")||
					methodName.contains("edit")||
					methodName.contains("Delete")||
					methodName.contains("Edit"))
			{
				/**
				 *
				 * 检查项改了,对套餐列表的数据没有影响,直接影响是套餐详情的数据,直接将redis内有关数据清空
				 * 检查项的改动相关方法: 增删改 ,但是新增一个检查项并不会对现有的套餐详情产生影响
				 * 同理检查组的修改也是这样的.
				 * 所以在当我进行删除和修改时才进行对套餐详情的删除操作
				 *
				 * */

				redisTemplate.delete("SETMEAL_DETAIL");

				/*Jedis resource = jedisPool.getResource();
				resource.del("SETMEAL_DETAIL");
				resource.close();*/
				System.out.println("环绕-后置正常通知：" + method.getName()+"执行数据变动相关方法,redis内数据被清除");


			}else{
				System.out.println("环绕-后置正常通知: "+method.getName()+"没有数据变动相关方法执行,原样执行,就当无事发生");
			}
			return object;
		} catch (Throwable throwable) {
			System.out.println("环绕-后置异常通知："+method.getName());
			throw new RuntimeException(throwable);
		}finally {
			System.out.println("环绕-最终通知："+method.getName());
		}
	}

	/**
	 * @param joinPoint  setmealServiceImpl内的所有方法
	 * @return java.lang.Object
	 * @author wuguang
	 * @date 2020/8/6 11:59
	 *
	 */

	@Override
	@Around("execution(* com.itheima.service.impl.SetmealServiceImpl.*(..))")
	public Object setMealServiceAround(ProceedingJoinPoint joinPoint) {
		//连接点获取方法签名
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();

		//取方法参数
		Object[] args = joinPoint.getArgs();

		//获取方法名
		String methodName = method.getName();


		try {
			System.out.println("环绕-前置正常通知:"+methodName+"开始被执行增强");

			//先对方法进行正常的执行操作
			Object object = joinPoint.proceed();
			//判断方法名只要和增删改有关就将redis内数据进行同步,后置环绕通知进行增强
			if(methodName.contains("Add")|| methodName.contains("add")){
				//当执行新增方法,对详情数据没有影响,只需要将套餐列表数据清空即可
				System.out.println("环绕-后置正常通知：" + method.getName());

				redisTemplate.delete("SETMEAL_LIST");
				/*Jedis resource = jedisPool.getResource();
				resource.del("SETMEAL_LIST");
				resource.close();*/
				System.out.println("环绕-后置正常通知：" + method.getName()+"执行数据变动相关方法,redis内SETMEAL_LIST数据被清除");
			}else if(methodName.contains("delete")||
					methodName.contains("Delete")||
					methodName.contains("edit")||
					methodName.contains("Edit"))
			{
				//当执行删除和修改方法,需要将redis上的套餐数据删除,同时将对应的套餐详情数据给删除
				Setmeal setmealArg = (Setmeal) args[0];
				Integer setmealId = setmealArg.getId();


				redisTemplate.delete("SETMEAL_LIST");
				redisTemplate.opsForHash().delete("SETMEAL_DETAIL","SETMEAL_DETAIL_"+setmealId);
				redisTemplate.opsForHash().delete("SIMPLE_SETMEAL_DETAIL","SIMPLE_SETMEAL_DETAIL_"+setmealId);


				//清除套餐列表数据
				/*Jedis resource = jedisPool.getResource();
				resource.del("SETMEAL_LIST");
				//清除hash结构内的对应的套餐记录
				resource.hdel("SETMEAL_DETAIL","SETMEAL_DETAIL_"+setmealId);
				resource.hdel("SIMPLE_SETMEAL_DETAIL","SIMPLE_SETMEAL_DETAIL_"+setmealId);
				//关闭资源
				resource.close();*/
			} else{
				System.out.println("环绕-后置正常通知: "+method.getName()+"没有数据变动相关方法执行,原样执行,就当无事发生");

			}
			return object;
		} catch (Throwable throwable) {
			System.out.println("环绕-后置异常通知："+method.getName());
			throw new RuntimeException(throwable);
		}finally {
			System.out.println("环绕-最终通知："+method.getName());
		}

	}


}
